package thread;

/**
 * 多线程模拟售票系统
 */
public class SellTicket {

    public static void main(String[] args) {

        /*
            3个售票窗口售出100张
         */

        SellTicket01 sellTicket01 = new SellTicket01();
        SellTicket01 sellTicket02 = new SellTicket01();
        SellTicket01 sellTicket03 = new SellTicket01();

        //启动3个售票线程
        //会出现超卖现象
        sellTicket01.start();
        sellTicket02.start();
        sellTicket03.start();

    }
}


//使用Thread方式
class SellTicket01 extends Thread {

    private static int num = 100;   //让多个线程共享 num

    @Override
    public void run() {
        while (true) {

            if (num <= 0) {
                System.out.println("售票结束...");
                break;
            }

            //休眠50ms，模拟人休息
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("窗口 " + Thread.currentThread().getName() + "售出1张票" + "剩余票数=" + (--num));
        }
    };
}